//
//  WelfarePageView.m
//  MindBox
//
//  Created by MS on 16-1-13.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#define SEL_VIEW_HEIGHT  CGRectGetMaxY(_headerView.frame)+10
#import "WelfarePageView.h"

#import "UIImageView+WebCache.h"
@interface WelfarePageView ()
@property (nonatomic,strong) NSMutableArray * strArray;

@end

@implementation WelfarePageView

-(void)setWelfareData:(Welfare_Detail *)welfareData
{
    _welfareData = welfareData;
    
    for (Search_Key_List * item in welfareData.search_key_list) {
        
        [self.strArray addObject:item.name];
    }
    self.gift_selectView.buttonsArray = _strArray;
    
    NSArray * bonus_url = welfareData.bonus_url;
    NSArray * welfare = welfareData.welfare;
    
    [self.headerView.imageView0 sd_setImageWithURL:[NSURL URLWithString:[welfare.lastObject header_url]]];
    
    [self.headerView.imageView1 sd_setImageWithURL:[NSURL URLWithString:bonus_url.lastObject]];
    
    [self layoutSubviews];
    [self.collectionView reloadData];
}

-(WelfareHeaderView *)headerView
{
    if (_headerView==nil) {
        WelfareHeaderView * view = [WelfareHeaderView welfareheadViewWithXib];
        _headerView = view;
        _headerView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [self.collectionView addSubview:view];
    }
    return _headerView;
}

-(NSMutableArray *)strArray
{
    if (_strArray==nil) {
        _strArray = [NSMutableArray array];
    }
    return _strArray;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(SEL_VIEW_HEIGHT+self.gift_selectView.height+10, 10, 0, 10);
    
    /**
     * _gift_selectView.frame设置动态变化，不可用以下设置
     */
    //    return UIEdgeInsetsMake(CGRectGetMaxY(_gift_selectView.frame)+10, 10, 0, 10);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /**
     *  设置选择条悬停
     */
    if (scrollView.contentOffset.y>= SEL_VIEW_HEIGHT) {
        
        self.gift_selectView.y =scrollView.contentOffset.y;
    }
    
    else{
        self.gift_selectView.y = SEL_VIEW_HEIGHT;
    }
}

-(void)selectViewDidClick:(SelectView *)selectView index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(giftSelectedViewDidClick:selectItem:index:)]) {
        [self.delegate giftSelectedViewDidClick:self selectItem:self.giftPageData.gift_cat_id[index] index:index];
    }
    
    /**
     *  选择条悬停时点击回到头部位置
     */
    if (selectView.y>CGRectGetMaxY(_headerView.frame)+10) {
        [self.collectionView setContentOffset:CGPointMake(0, SEL_VIEW_HEIGHT)animated:YES];
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _headerView.frame  = CGRectMake(0, 0, self.width, 350);

    self.gift_selectView.frame = CGRectMake(0, SEL_VIEW_HEIGHT, self.width, 35);

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
